//
// Created by Tian on 25-5-2.
//
#include <stdio.h>
#include <esp_log.h>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "fan.h"
#include "fan_led.h"
#include "fan_pcnt.h"
#include "fan_pwm.h"

struct fan_t
{
    int curRPM;
    int curDuty;
};

static struct fan_t fan[2];

void fan_task(void* arg)
{
    fan_pwm_task_init(1);
    fan_pcnt_task_init();
    fan_led_init();

    while(1)
    {
        fan[0].curRPM = fan_get_rpm_by_pcnt();
        fan_pwm_set_duty(0,fan[0].curDuty);
        led_set_light(fan[0].curRPM);
        //ESP_LOGI(__FILE__, "curRPM:%d, curDuty:%d.",fan[0].curRPM, fan[0].curDuty);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

int fan_get_rpm(int f)
{
    return fan[0].curRPM;
}

void fan_set_duty(int f, int duty)
{
    fan[0].curDuty = duty;
    //fan[0].curDuty = (int)(((float )fan[0].curRPM / 1800.0f) * 100.0f);
}

void fan_task_init(void)
{
    xTaskCreate(fan_task, "fan_task", 4096, NULL, 10, NULL);
}
